# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: net_tcp_server_1.py 
@time: 2019/7/11 上午10:28
"""
'''
创建TCP的server服务
使用StreamRequestHandler基类
使用ForkingTCPSever或ThreadingTCPServer对象创建多线程服务
'''

from socketserver import StreamRequestHandler, ThreadingTCPServer


class EchoHandler(StreamRequestHandler):
    def handle(self):
        print('Got connection from', self.client_address)
        while True:
            msg = self.request.recv(8192)
            if not msg:
                break
            self.request.send(msg)
        # for line in self.rfile:
        #     self.wfile.write(line)
        #     print(line)




if __name__ == '__main__':
    # 允许服务器重新绑定之前使用过的端口
    ThreadingTCPServer.allow_reuse_address = True
    serv = ThreadingTCPServer(('', 20000), EchoHandler)
    serv.serve_forever()